package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Persona;
import edu.upc.clase.demo.service.PersonaService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Carlos
 */
@Controller("personaController")
public class PersonaController {

    @Autowired
    private PersonaService personaService;
    private static Logger log = LoggerFactory.getLogger(PersonaController.class);

    @RequestMapping("/personas/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("personas/index");
        List<Persona> personas = personaService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("personas", personas);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/personas/new", method = RequestMethod.GET)
    public ModelAndView newPersona() {
        ModelAndView mav = new ModelAndView("personas/new");
        Persona persona = new Persona();
        mav.getModelMap().put("persona", persona);
        return mav;
    }

    @RequestMapping(value = "/personas/new", method = RequestMethod.POST)
    public String createPersona(@ModelAttribute("persona")Persona persona, SessionStatus status) {    
        personaService.insertar(persona);
        status.setComplete();
        return "redirect:/pages/personas/index";
    }
    
    @RequestMapping(value = "/personas/edit", method = RequestMethod.GET)
    public ModelAndView editPersona(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("personas/edit");
        Persona persona = personaService.buscar(id);
        mav.getModelMap().put("persona", persona);
        return mav;        
    }
    
    @RequestMapping(value="/personas/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("persona") Persona persona, SessionStatus status) {
        personaService.actualizar(persona);
        status.setComplete();
        return "redirect:/pages/personas/index";
    }
    
    @RequestMapping("/personas/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/personas/index");
        Persona persona = personaService.buscar(id);
        personaService.eliminar(persona);
        return mav;
    }
    
    @RequestMapping(value = "/personas/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("personas/resultadoBusqueda");
        log.info("resultado = " + personaService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("resultado",personaService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }
    
}
